﻿using DADAL;
using SqlSugar.Extensions;

namespace DABLL
{
    public class DevicePoint
    {

        /// <summary> 添加设备点位
        /// 
        /// </summary>
        /// <param name="obj"></param>
        public static Response AddDevicePoint(DevicePointModel obj)
        {
            Response response = Response.DefaultResponse();
            bool res = DevicePointModel.RecordExistenceWithName(obj.Name, obj.DeviceNumber);
            if (res)
            {
                response.Msg = string.Format("【{1}】点位已存在", obj.Name);
                return response;
            }

            if (obj.Address.ObjToInt() > 0)
            {
                obj.Address = (obj.Address.ObjToInt() - 1).ToString();
            }
            try
            {
                obj.Address = Encryption.DesEncrypt(obj.Address, SqlSugarHelper.EncryptionKey);
            }
            catch (Exception)
            {
                response.Msg = "请重新输入设备地址";
                return response;
            }
            List<DevicePointModel> list = new List<DevicePointModel>();
            list.Add(obj);
            int result = SqlSugarHelper.Insertable(list);
            if (result > 0)
            {
                response.Code = 200;
                response.Msg = string.Format("已添加【{0}】点位", obj.Name);
            }
            else
            {
                response.Msg = string.Format("点位添加失败");
            }
            return response;
        }

        /// <summary>
        /// 删除设备点位
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static Response DeleteDevicePoint(int number)
        {
            Response response = Response.DefaultResponse();
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = number;
            List<DevicePointModel> list = SqlSugarHelper.Inquireable<DevicePointModel>(where);
            if (list.Count > 0)
            {
                int deleteRes = SqlSugarHelper.Deleteable(list);
                if (deleteRes > 0)
                {
                    response.Code = 200;
                    response.Msg = string.Format("设备点位已删除");
                }
                else
                {
                    response.Msg = string.Format("设备点位删除失败");
                }
                return response;
            }
            else
            {
                response.Msg = string.Format("【{0}】设备点位不存在，无法删除", number);
            }
            return response;
        }

        /// <summary>
        /// 设备点位列表
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="page">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        public static List<DevicePointModel> DevicePointList(Dictionary<string, Object>? where,int page, int pageSize, ref int totalCount)
        {
            List<DevicePointModel> list = SqlSugarHelper.InquireableToPageList<DevicePointModel>(where, page, pageSize, ref totalCount);
            return list;
        }
    }
}
